<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        thead {
            background-color: blue;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <tr>
                <td><input type="checkbox" name="" id="thead"></td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
            </tr>
        </tbody>
    </table>
    <script>
        var ths = document.querySelector('#thead');
        var tbs = document.querySelector('tbody').querySelectorAll('input');
        ths.onclick = function () {
            console.log(this.checked);
            for (let i = 0; i < tbs.length; i++) {
                tbs[i].checked = this.checked;
            }
        }

        for (var i = 0; i < tbs.length; i++) {
            tbs[i].onclick = function () {
                var flag = true;
                for (let i = 0; i < tbs.length; i++) {
                    if (!tbs[i].checked) {
                        flag = false;
                        break;
                    }
                }
                ths.checked = flag;
            }
        }
    </script>
</body>

</html>